package yb.ecp.fast.user.infra.datainit;

import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StreamUtils;
import yb.ecp.fast.infra.infra.log.LogHelper;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * @author wanghao
 * @Description 读取脚本工具类
 * @date 2018-02-28 16:11
 */
public class ScriptLoader {
    private ScriptLoader() {
    }
    public static final String SPLIT = ";";

    public static String[] load(String filePath) {
        InputStream fs = null;
        String result = null;
        try {
            fs = new ClassPathResource(filePath).getInputStream();
            result = StreamUtils.copyToString(fs, StandardCharsets.UTF_8);
        } catch (FileNotFoundException e) {
            LogHelper.fatal("cannot find file", e);
        } catch (IOException e) {
            LogHelper.fatal(e.getMessage(), e);
        } finally {
            try {
                if (fs != null) {
                    fs.close();
                }
            } catch (IOException e) {
                LogHelper.fatal(e.getMessage(), e);
            }
        }
        if(result == null){
            return new String[]{};
        }else {
            return result.split(SPLIT);
        }
    }
}
